package cate.game.role;

import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import lombok.Getter;

/**
 * 检测功能开启
 */
public abstract class FuncOpenChecker extends RoleDependent implements IOpenChecker{
    @NoteField(value = "是否开启")
    @JsonIgnore
    @Getter
    protected boolean open;

    @NoteField("上次合服处理的时间")
    @JsonIgnore
    @Getter
    protected long mergeSettledTime;

    public FuncOpenChecker(){

    }

    public FuncOpenChecker(Role role){
        super(role);
    }

    @Override
    public void initialize(Role role){
        super.initialize(role);
    }

    @Override
    public void onEnter(){
        if (mergeSettledTime == role.getGame().getMergeTime()) {
            return;
        }
        if (isOpen() && mergeSettledTime < role.getGame().getMergeTime()) {
            try{
                onMergeSettled();
                mergeSettledTime = role.getGame().getMergeTime();
            } catch (Exception e) {
                logger.error("{}功能合服处理失败,", getFuncId(), e);
            }
        } else {
            mergeSettledTime = role.getGame().getMergeTime();
        }
    }

    protected void onMergeSettled() {

    }

    public abstract int getFuncId();

    public abstract void onAfterOpen();

    @Override
    public void checkOpen(){
        if(open || role == null){
            return;
        }
        open = role.getGame().table.common.funcEnable.isEnable(
                role.getBase().level,
                role.getBase().vip,
                role.getGame().getOpenDay(),
                role.getGame().cross.getWorldLevel(),
                getFuncId(),
                role.getFarm().adventure.getAdventurePassedNum()
        ).ok() && customizeCheckOpen();
        if(open){
            onAfterOpen();
        }
    }

    public boolean customizeCheckOpen(){
        return true;
    }
}
